package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo32 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result=0;
                for (int i = 1; i <= 1000; i++) {
                    result += i;
                }
                return result;
            }
        };
        //Thread t=new Thread(callable);  //error,Thread没有提供构造函数来传入callable
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        Thread t=new Thread(futureTask);
        t.start();
        //接下来这个代码不需要join，使用futureTask获取到结果
        System.out.println(futureTask.get());
        //futureTask.get()这个操作也是带有阻塞功能的，如果线程还没执行完毕，get就会阻塞，等到线程执行完了，return的结果就会被get返回回来
    }
}
